// mircoApp/meituan_refund/search/search.js
const skinBehavior = require('../../../utils/themeSkin/skinBehavior');
const app = getApp();
var QQMapWX = require('../../../mymap/qqmap-wx-jssdk.min.js');
var qqmapsdk;
Page({

  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    globalData: {
      navHeight: 0,
      navTop: 0,
      jnheight: 0,
      jnwidth: 0,
    },
    keyword: "",

    list: [],
    page: 1,
    last_page: 1,

    sId: "",
    lng: "",
    lat: "",
    cityId: "",
    cate_id: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let address_point = wx.getStorageSync("address-point");
    let language = wx.getStorageSync('langIndex');
    this.setData({
      'language': language.en,
    });
    if(address_point){
      this.data.lng = address_point.point.lng;
      this.data.lat = address_point.point.lat;
    }
    this.initNavBar();
    this.setData({
      city_name: options.city
    });
    if(options.path=='home'){
      this.setData({keyword: options.k});
      let field = ["cate_id", "cityId", "lat", "lng", "sId"];
      field.forEach(item => {
        if (item == 'cate_id' || item == 'sId') {
          let meituanCategory = wx.getStorageSync('meituanCategory');
          this.data[item] = meituanCategory[item];
        } else if (options[item]) {
          this.data[item] = options[item];
        }
      })
      this.getData()
   
    }else{
      this.data.cityId = options.cityId;
      wx.setStorageSync('meituan_city_id', options.cityId)
      this.getCategory();
    }
  },
  getCategory() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.category'),
      data: {
        city_id: this.data.cityId
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        this.data.cate_id = data[0].id;
        this.getChildrenCategory(data[0].id)
      }
    })
  },
  getChildrenCategory(first_cate_id) {
    app._getNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.category'),
      data: {
        city_id: this.data.cityId,
        first_cate_id
      },
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result !== 1) {
          return app.tips(msg);
        }
        this.data.sId = data[0].id;
        this.getData();
      }
    })
  },
  goto_detail(e) {
    let data = e.currentTarget.dataset.item;
    wx.setStorageSync('meituanCategory', {
      'cate_id':this.data.cate_id
    })
    wx.setStorageSync("meituan_dealid",data.dealBaseInfo.dealGroupId);
    wx.navigateTo({
      url: `/mircoApp/meituan_refund/detail/detail?path=home&dealid=${data.dealBaseInfo.dealGroupId}&cate_id=${this.data.cate_id}&cityId=${this.data.cityId}&lng=${this.data.lng}&lat=${this.data.lat}`
    })
  },
  getData(page = 1) {
    let json = {
      page,
      first_cate_id: this.data.cate_id,
      keyword: this.data.keyword,
      geo_info: {
        cityId: "",
        lat: "",
        lng: ""
      }
    }
    if(this.data.sId>0){
      json.second_cate_id = this.data.sId;
    }
    if (this.data.lng || this.data.lat) {
      json.geo_info.lat = this.data.lat;
      json.geo_info.lng = this.data.lng;
    }
    if (this.data.cityId) {
      json.geo_info.cityId = this.data.cityId
    }


    app._postNetWork({
      url: app.getNetAddresss('plugin.meituan-group-buy.frontend.good.good'),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
          data
        } = resdata.data;
        if (result) {
          if (data.current_page == 1) {
            this.setData({
              list: data.records
            })
          } else if (data.current_page > 1) {
            this.data.list = this.data.list.concat(data.records)
            this.setData({
              list: this.data.list
            })
          }
          this.data.page = data.current_page;
          this.data.last_page = data.last_page;

        } else {
          app.tips(msg);
        }
      }
    })
  },
  InputHandle(e) {
    this.setData({
      keyword: e.detail.value
    });
  },
  initNavBar() {
    //获取菜单按钮的布局位置信息
    let menuButtonObject = wx.getMenuButtonBoundingClientRect();
    //获取系统信息
    wx.getSystemInfo({
      success: res => {
        //状态栏的高度
        let statusBarHeight = res.statusBarHeight,
          //胶囊按钮与顶部的距离
          navTop = menuButtonObject.top,
          navHeight = statusBarHeight + menuButtonObject.height + (menuButtonObject.top - statusBarHeight) * 2;
        let globalData = {};
        globalData.navHeight = navHeight; //导航栏高度
        globalData.navTop = navTop; //胶囊按钮与顶部的距离
        globalData.jnheight = menuButtonObject.height; //胶囊的高度
        globalData.jnwidth = menuButtonObject.width; //胶囊的宽度
        this.setData({
          globalData
        })
      },
      fail(err) {
        console.log(err);
      }
    });
  },
  navBack: function () {
    wx.navigateBack()
  },
  search() {
    this.getData();
  },
  navHome: function () {
    wx.navigateTo({
      url: '/packageI/citypickerPage/citypickerPage?plu=meituan&path=search'
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.page >= this.data.last_page) {
      return;
    }
    let page = this.data.page + 1;
    this.getData(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})